package com.ttg.common.utils;

import cn.hutool.core.util.ObjectUtil;

public class SensitivityUtil {
    public static String hideChineseName(String chineseName){
        if (ObjectUtil.isNull(chineseName)){
            return null;
        }
        return desValue(chineseName,1,0,"*");
    }

    public static String hideIdCard(String idCard){
        return idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})","$1****$2");
    }

    public static String phone(String phone){
        if (phone.length()<=10) {
            return phone.replaceAll("(\\d{0})\\d{5}(\\w{2})", "$1*****$2");
        }else {
            return phone.replaceAll("(\\d{3})\\d{4}(\\w{4})","$1****$2");
        }
    }

    public static String email(String email){
        return email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)","$1****$3$4");
    }

    public static String desValue(String origin,int prefixNoMaskLen,int suffixNoMaskLen,String maskStr){
        if (ObjectUtil.isNull(origin)){
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < origin.length(); i++) {
            if (i<prefixNoMaskLen){
                sb.append(origin.charAt(i));
                continue;
            }
            if (i>origin.length()-suffixNoMaskLen-1){
                sb.append(origin.charAt(i));
                continue;
            }
            sb.append(maskStr);
        }
        return sb.toString();
    }
}
